package com.dhecp.project.business.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.core.utils.ExportExcelUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.framework.web.service.BaseInfoSimpleService;
import com.dhecp.project.base.domain.BaseInfo;
import com.dhecp.project.business.domain.ProductBasis;
import com.dhecp.project.business.domain.ProductBasisCompete;
import com.dhecp.project.business.domain.ProductBasisComponent;
import com.dhecp.project.business.service.ProductBasisService;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.system.domain.SysDictionaryForExport;
import com.dhecp.project.system.domain.SysDictionaryForPageField;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 商品库 控制层
 * @author DH-Xxj
 * @version 2022年7月18日
 */
@Controller
@RequestMapping("/business/productbasis")
public class ProductBasisController extends BaseModuleController{

	private String prefix = "business/productbasis";
    private String moduleTable = ModuleTable.PRODUCT_BASIS;
    private String moduleTableZJ = ModuleTable.PRODUCT_BASIS_COMPONENT;
    private String moduleTableJP = ModuleTable.PRODUCT_BASIS_COMPETE;
    
    @Autowired
    private ProductBasisService productBasisService;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;
    
    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;

    @Autowired
    private BaseInfoSimpleService baseInfoSimpleService;
    
    /**
     * 打开商品库列表
     * @param params
     * @return
     * @author DH-Xxj
     */
	@RequiresPermissions("business:productbasis:view")
    @GetMapping()
    public ModelAndView dataList(@RequestParam Map<String, Object> params){
		params.put("moduleTable", moduleTable);
    	params.put("entity", new ProductBasis()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	Subject userSubject = ShiroUtils.getSubject();
    	Integer toolBarWidth = 0;
    	if (userSubject.isPermitted("business:productbasis:delete")){	//当前登录用户主体是否有删除的权限
        	params.put("checkBox", "yes"); //生成表格gridColumns时包含复选
        	toolBarWidth = toolBarWidth + 100;
    	}
    	if (userSubject.isPermitted("business:productbasis:edit")){ //当前登录用户主体是否有修改的权限
    		toolBarWidth = toolBarWidth + 100;
    	}
    	params.put("toolBarWidth", toolBarWidth);

    	Object importFields = null;
    	Object importFieldsZJ = null;
		ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        Map<String, Object> dictionaryForFieldMapZJ = new HashMap<String, Object>();
        Map<String, Object> gridColumns = new HashMap<String, Object>();
        Map<String, Object> gridColumnsZJ = new HashMap<String, Object>();
        try{
        	importFields = dictionaryForFieldService.queryFieldFrontImport(params); //商品导入模板
            dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
            gridColumns = gridColumnsPropertyModel.queryGridColumnsPropertyModel(params);
            
    		params.put("moduleTable", moduleTableZJ);
        	params.put("entity", new ProductBasisComponent()); 
        	importFieldsZJ = dictionaryForFieldService.queryFieldFrontImport(params); //子件导入模板
        	dictionaryForFieldMapZJ = dictionaryForFieldService.queryFieldAttributesMap(params);
        	gridColumnsZJ = gridColumnsPropertyModel.queryGridColumnsPropertyModel(params);
            
            if (dictionaryForFieldMap.isEmpty() || dictionaryForFieldMapZJ.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        
        //商品列表属性
        mv.addObject("moduleTable", moduleTable);
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("gridColumns", gridColumns);
        mv.addObject("importFields",importFields);				//商品导入字段模板
        
        //子件列表属性
        mv.addObject("moduleTableZJ", moduleTableZJ);
        mv.addObject("fieldsZJ", TypeUtils.mapToJson(dictionaryForFieldMapZJ));
        mv.addObject("gridColumnsZJ", gridColumnsZJ);
        mv.addObject("importFieldsZJ",importFieldsZJ);			//子件导入字段模板
        
        mv.addObject("ajaxResult", ajaxResult);
        mv.setViewName(prefix + "/productbasis");
		return mv;
    }
	
	/**
     * 打开产品选择页面
     * @param params
     * @return ModelAndView
     * @author DH-Xxj
     */
    @GetMapping("/select")
    public ModelAndView selectList(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
    	params.put("entity", new ProductBasis()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	params.put("toolBarWidth", 0);
    	if(params.get("pagetype") !=null && params.get("pagetype").toString().equalsIgnoreCase("checkbox"))params.put("checkBox", "yes");
        else params.put("radio", "yes");
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("ajaxResult", ajaxResult);
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
        mv.addObject("prefix", prefix);
        mv.addObject("moduleTable", moduleTable);
        mv.setViewName("common/select");
        return mv;
    }

	/**
     * 打开产品选择页面
     * @param params
     * @return ModelAndView
     * @author DH-Xxj
     */
    @GetMapping("/selectSon")
    public ModelAndView selectSonList(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTableZJ);
    	params.put("entity", new ProductBasisComponent()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	params.put("toolBarWidth", 0);
    	if(params.get("pagetype") !=null && params.get("pagetype").toString().equalsIgnoreCase("checkbox"))params.put("checkBox", "yes");
        else params.put("radio", "yes");
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("ajaxResult", ajaxResult);
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
        mv.addObject("prefix", prefix);
        mv.addObject("moduleTable", moduleTableZJ);
        mv.setViewName("common/select");
        return mv;
    }
	
	/**
	 * 查询商品库
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/list")
    @RequiresPermissions("business:productbasis:list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> params){
		params.put("moduleTable", moduleTable);
        List<ProductBasis> list = new ArrayList<ProductBasis>();
        try {
        	list = productBasisService.queryProductBasis(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }


	/**
	 * 查询全部商品子件
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/listson")
    @RequiresPermissions("business:productbasis:list")
    @ResponseBody
    public TableDataInfo listson(@RequestParam Map<String, Object> params){
		params.put("moduleTable", moduleTableZJ);
        List<ProductBasisComponent> list = new ArrayList<ProductBasisComponent>();
        try {
        	list = productBasisService.queryProductBasisComponent(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }

	/**
	 * 查询指定商品子件
	 * @param pcode 必要参数：母件编码【pcode】，即商品库的商品编码【code】。
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/listzj")
    @RequiresPermissions("business:productbasis:list")
    @ResponseBody
    public TableDataInfo listzj(@RequestParam Map<String, Object> params){
		String pcode = "nopcode";
		if (params.get("pcode") == null){
			return getDataTable("请提供母件编码【pcode】（商品库的商品编码【code】）。");
		}else{
			pcode = params.get("pcode").toString();
		}
		params.put("moduleTable", moduleTableZJ);
        List<ProductBasisComponent> list = new ArrayList<ProductBasisComponent>();
        try {
        	list = productBasisService.queryProductBasisComponentBypcode(pcode);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }

	@GetMapping("/listzj2")
    @RequiresPermissions("business:productbasis:list")
    @ResponseBody
    public TableDataInfo listzj2(@RequestParam Map<String, Object> params){
		params.put("moduleTable", moduleTableZJ);
        List<ProductBasisComponent> list = new ArrayList<ProductBasisComponent>();
        try {
            if (params.get("pcode") != null && params.get("pcode") != ""){
                String wherestrhand = "t1.pcode in ('" + params.get("pcode").toString().replace(",", "','") + "')";
                params.put("wherestrhand", wherestrhand);
                try{
                    list = productBasisService.queryProductBasisComponentBypcode2(params);
                }catch(Exception e){
                    return getDataTable(this.getExceptionMessage(e).toString());
                }
            }else{
                return getDataTable("请传入已选中的商品编码字符串（pcode），取值示例：0000,1001,1002");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }

    /**
    *@Description: 查询已选择的商品
    *@Param: selectedUser ：已选中的商品编号字符串，示例：0000,1001,1002
    *@return: com.dhecp.common.core.page.TableDataInfo
    *@Author: hlf
    *@date: 2022-08-18 17:13
    */
    @GetMapping("/listselect")
    @ResponseBody
    public TableDataInfo userInfoSelectListJson(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        List<ProductBasis> list = new ArrayList<ProductBasis>();
        if (params.get("selectedID") != null && params.get("selectedID") != ""){
            String wherestrhand = "t1.code in ('" + params.get("selectedID").toString().replace(",", "','") + "')";
            params.put("wherestrhand", wherestrhand);
            try{
                list = productBasisService.queryProductBasisList(params);
            }catch(Exception e){
                return getDataTable(this.getExceptionMessage(e).toString());
            }
        }else{
            return getDataTable("请传入已选中的商品编码字符串（selectedUser），取值示例：0000,1001,1002");
        }
        return getDataTable(list);
    }
	
	/**
	 * 查询竞品库
	 * @param pcode 必要参数：母件编码【pcode】，即商品库的商品编码【code】。
	 * @return
	 * @author DH-Xxj
	 */
	@GetMapping("/listjp")
    @RequiresPermissions("business:productbasis:list")
    @ResponseBody
    public TableDataInfo listjp(@RequestParam Map<String, Object> params){
		String pcode = "nopcode";
		if (params.get("pcode") == null){
			return getDataTable("请提供母件编码【pcode】（商品库的商品编码【code】）。");
		}else{
			pcode = params.get("pcode").toString();
		}
		params.put("moduleTable", moduleTableJP);
        List<ProductBasisCompete> list = new ArrayList<ProductBasisCompete>();
        try {
        	list = productBasisService.queryProductBasisCompeteBypcode(pcode);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }
	
	 /**
     * 导出商品库
	  * @param params
	  * @return
	  * @throws Exception
	  * @author DH-Xxj
	  */
    @Log(title = "商品导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("business:productbasis:export")
    @GetMapping("/export")
    @ResponseBody
    public AjaxResult export(@RequestParam Map<String, Object> params) throws Exception{
		params.put("moduleTable", moduleTable);
    	params.put("entity", new ProductBasis()); 
        List<ProductBasis> list = productBasisService.queryProductBasis(params);
//        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
    	params.put("getFieldAttributesType", "export");
    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<ProductBasis> util = new ExportExcelUtils<ProductBasis>(moduleTable,new ProductBasis());
        return util.exportExcel(list, fieldList, "商品数据", "export");
    }
    
    /**
     * 导出Excel导入模板
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @RequiresPermissions("business:productbasis:import")
    @Log(title = "商品导出", businessType = BusinessType.EXPORT)
    @GetMapping("/exporttemplate")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult exportTemplate(@RequestParam Map<String, Object> params) throws Exception{
		params.put("moduleTable", moduleTable);
    	params.put("entity", new ProductBasis()); 
    	params.put("getFieldAttributesType", "template");

    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<ProductBasis> util = new ExportExcelUtils<ProductBasis>(moduleTable,new ProductBasis());
        return util.exportExcel(null, fieldList, "商品导入模板", "template");
    }

	
	/**
	 * 导出商品子件
	 * @param params
	 * @return
	 * @throws Exception
	 * @author DH-Xxj
	 */
    @Log(title = "商品子件导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("business:productbasis:export")
    @GetMapping("/exportzj")
    @ResponseBody
    public AjaxResult exportZJ(@RequestParam Map<String, Object> params) throws Exception{
		params.put("moduleTable", moduleTableZJ);
		params.put("entity", new ProductBasisComponent()); 
		List<ProductBasisComponent> list = productBasisService.queryProductBasisComponent(params);
		params.put("getFieldAttributesType", "export");
		List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
		ExportExcelUtils<ProductBasisComponent> util = new ExportExcelUtils<ProductBasisComponent>(moduleTableZJ,new ProductBasisComponent());
		return util.exportExcel(list, fieldList, "商品子件数据", "export");
    }

    /**
     * 导出商品子件Excel导入模板
     * @param params
     * @return
     * @throws Exception
     * @author DH-Xxj
     */
    @RequiresPermissions("business:productbasis:import")
    @Log(title = "商品导出子件", businessType = BusinessType.EXPORT)
    @GetMapping("/exporttemplateZJ")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult exportTemplateZJ(@RequestParam Map<String, Object> params) throws Exception{
		params.put("moduleTable", moduleTableZJ);
    	params.put("entity", new ProductBasisComponent());
    	params.put("getFieldAttributesType", "template");

    	List<SysDictionaryForExport> fieldList = dictionaryForFieldService.queryExportFieldList(params);
        ExportExcelUtils<ProductBasisComponent> util = new ExportExcelUtils<ProductBasisComponent>(moduleTableZJ,new ProductBasisComponent());
        return util.exportExcel(null, fieldList, "商品子表导入模板", "template");
    }
	
	/**
     * 打开商品库新增页
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
    @RequiresPermissions("business:productbasis:add")
    @GetMapping("/add")
    public ModelAndView add(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
    	params.put("entity", new ProductBasis()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
		List<SysDictionaryForPageField> dicFieldsList = new ArrayList<SysDictionaryForPageField>(); //字段字典
		List<Object> fieldsList = new ArrayList<Object>(); 			//生成控件的字段和属性
        List<Object> fieldsHiddenList = new ArrayList<Object>(); 	//隐藏的字段和属性
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        try{
            dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
            if (dictionaryForFieldMap.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            }

        	params.put("getFieldAttributesType", "pagenotingrid"); 	//非表格字段
    		dicFieldsList = dictionaryForFieldService.queryFieldAttributesList(params);
            if (dicFieldsList.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
            	for (SysDictionaryForPageField dicFields : dicFieldsList){
                    Map<String, Object> fieldsListMap = TypeUtils.entityToMap(dicFields);
                    fieldsListMap.put("fieldContent", "");

                    //如果有下拉控件，则根据配置获取对应下拉待选内容
                    List<BaseInfo> dropdownList = new ArrayList<BaseInfo>();
                    String fieldFrontType = dicFields.getFieldFrontType();
                    String fieldSearchType = dicFields.getFieldSearchType();
                    String fieldFrontSave = dicFields.getFieldFrontSave();
                    String fieldFrontShow = dicFields.getFieldFrontShow();
                    if (fieldFrontType != null && fieldFrontType.toLowerCase().startsWith("t_dropdown")){
                        if (fieldSearchType != null && !"".equals(fieldSearchType)){
                            if (fieldFrontSave == null || "".equals(fieldFrontSave)){
                                fieldFrontSave = "vid";
                            }
                            if (fieldFrontShow == null || "".equals(fieldFrontShow)){
                                fieldFrontShow = "vname";
                            }
                            dropdownList = baseInfoSimpleService.getDropdownData(fieldSearchType,fieldFrontSave,fieldFrontShow);
                        }
                    }
                    fieldsListMap.put("dropdownList", dropdownList);

            		if (fieldsListMap.get("fieldFrontType") == null){
            			fieldsListMap.put("fieldFrontType", "t_text");
            		}
            		
            		if ((Integer)fieldsListMap.get("fieldFrontTop") == 1){	//显示的字段
            			fieldsList.add(TypeUtils.mapToJson(fieldsListMap));
            		}else {	//隐藏的字段
            			fieldsHiddenList.add(TypeUtils.mapToJson(fieldsListMap));
            		}
            	}
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        mv.addObject("pageType","add"); //页面类型：新增（add）、修改（edit）
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap)); //各字段及属性
        mv.addObject("fieldsList", fieldsList);				//显示的字段
        mv.addObject("fieldsHiddenList", fieldsHiddenList);	//隐藏的字段
        mv.addObject("ajaxResult", ajaxResult);
        mv.setViewName(prefix + "/productbasis_add");
        return mv;
    }
    
    /**
     * 新增商品库——数据提交
     * @param params
     * @return
     * @author DH-Xxj
     */
    @Log(title = "保存商品", businessType = BusinessType.INSERT)
    @RequiresPermissions("business:productbasis:add")
    @PostMapping("/addpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(@RequestParam Map<String, Object> params){
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.insertProductBasis(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 新增商品子件——数据提交
     * @param params
     * @return
     * @author DH-Xxj
     */
    @Log(title = "保存商品子件", businessType = BusinessType.INSERT)
    @RequiresPermissions("business:productbasis:add")
    @PostMapping("/addzjpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addZJSave(@RequestParam Map<String, Object> params){
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.insertProductBasisComponent(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
	 * 保存商品子件（先删除后新增）——数据提交
	 * @param pcode 必要参数：商品库中的商品编码（code）
	 * @param productBasisComponentItems 必要参数：商品子件对应实体类（ProductBasisComponent）的json格式字符串
     * @return
     * @author DH-Xxj
     */
    @Log(title = "保存商品子件", businessType = BusinessType.INSERT)
    @RequiresPermissions("business:productbasis:edit")
    @PostMapping("/savezjpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult saveZJ(@RequestParam Map<String, Object> params){
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.saveProductBasisComponent(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
	 * 保存竞品库（先删除后新增）——数据提交
	 * @param pcode 必要参数：商品库中的商品编码（code）
	 * @param productBasisCompeteItems 必要参数：竞品库对应实体类（ProductBasisCompete）的json格式字符串
     * @return
     * @author DH-Xxj
     */
    @Log(title = "保存竞品库", businessType = BusinessType.INSERT)
    @RequiresPermissions("business:productbasis:edit")
    @PostMapping("/savejppost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult saveJP(@RequestParam Map<String, Object> params){
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.saveProductBasisCompete(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 清理商品导入过渡表中遗留的数据
     * @param params 无需参数
     * @return
     * @author DH-Xxj
     */
    @RequiresPermissions("business:productbasis:add")
    @PostMapping("/cleanproductimportpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult cleanProductBasisImport(@RequestParam Map<String, Object> params){
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.cleanProductBasisImport(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 将本次导入的商品数据从商品库导入过渡表中转入商品库正式表
     * @param records 导入总记录数int类型
     * @return
     * @author DH-Xxj
     */
    @Log(title = "商品转移至正式表", businessType = BusinessType.INSERT)
    @RequiresPermissions("business:productbasis:add")
    @PostMapping("/importtoprocuctpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult importToProductBasis(@RequestParam Map<String, Object> params){
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.importToProductBasis(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 导入商品信息至商品库过渡表——数据提交
	 * @param productBasisItems 必要参数：基础商品库对应实体类（ProductBasis）的json格式字符串
     * @return
     * @author DH-Xxj
     */
    @Log(title = "商品过渡表导入", businessType = BusinessType.INSERT)
    @RequiresPermissions("business:productbasis:import")
    @PostMapping("/importpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult importSave(@RequestParam Map<String, Object> params){
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.importProductBasis(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 打开商品库修改页
     * @param enableEdit 可选参数：当前页面是否可编辑（0：不可编辑，1：可编辑）
     * @return
     * @author DH-Xxj
     */
//    @RequiresPermissions(value={"business:productbasis:edit","business:productbasis:view"},logical=Logical.OR)
    @GetMapping("/edit")
    public ModelAndView menuInfoEdit(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
    	params.put("entity", new ProductBasis()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
		List<SysDictionaryForPageField> dicFieldsList = new ArrayList<SysDictionaryForPageField>(); //字段字典
		List<Object> fieldsList = new ArrayList<Object>(); 			//生成控件的字段和属性
        List<Object> fieldsHiddenList = new ArrayList<Object>(); 	//隐藏的字段和属性
        ProductBasis productBasis = new ProductBasis();
        Subject userSubject = ShiroUtils.getSubject();
        Integer enableEdit = 0;
        Object importFields = null;
        if (params.get("enableEdit") == null || "1".equals(params.get("enableEdit").toString())){
        	if (userSubject.isPermitted("business:productbasis:edit")){ //当前登录用户主体是否有修改的权限
        		enableEdit = 1;
        	}
        }
        if (params.get("code") == null || params.get("code") == ""){
            mv = this.gotoModelAndViewErrorPage(params, "guid", "商品行唯一值");	
        }
        if (mv.isEmpty()){
            try{
                dictionaryForFieldMap = dictionaryForFieldService.queryFieldAttributesMap(params);
                if (dictionaryForFieldMap.isEmpty()){
                    ajaxResult = AjaxResult.error("未获取到字段属性。");
                }
            	/*else{
                	productBasis = productBasisService.queryProductBasisByGuid(params.get("guid").toString());
                	if (productBasis == null){
                		ajaxResult = AjaxResult.error("没有查到商品信息。");
                	}else{
                    	ajaxResult = AjaxResult.success("数据获取成功。");
                	}
                }
                */
            	params.put("getFieldAttributesType", "pagenotingrid"); 	//非表格字段
        		dicFieldsList = dictionaryForFieldService.queryFieldAttributesList(params);
                if (dicFieldsList.isEmpty()){
                    ajaxResult = AjaxResult.error("未获取到字段属性。");
                }else{
                	if (params.get("code") == null || params.get("code") == ""){
                		productBasis = productBasisService.queryProductBasisByGuid(params.get("guid").toString());
                	}else{
                		productBasis = productBasisService.queryProductBasisByCode(params.get("code").toString());
                	}
                	
                	if (productBasis == null){
                		ajaxResult = AjaxResult.error("没有查到商品信息。");
                	}else{
                    	ajaxResult = AjaxResult.success("数据获取成功。");
                    	for (SysDictionaryForPageField dicFields : dicFieldsList){
                            Map<String, Object> fieldsListMap = TypeUtils.entityToMap(dicFields);

                            //如果有下拉控件，则根据配置获取对应下拉待选内容
                            List<BaseInfo> dropdownList = new ArrayList<BaseInfo>();
                            String fieldFrontType = dicFields.getFieldFrontType();
                            String fieldSearchType = dicFields.getFieldSearchType();
                            String fieldFrontSave = dicFields.getFieldFrontSave();
                            String fieldFrontShow = dicFields.getFieldFrontShow();
                            if (fieldFrontType != null && fieldFrontType.toLowerCase().startsWith("t_dropdown")){
                                if (fieldSearchType != null && !"".equals(fieldSearchType)){
                                    if (fieldFrontSave == null || "".equals(fieldFrontSave)){
                                        fieldFrontSave = "vid";
                                    }
                                    if (fieldFrontShow == null || "".equals(fieldFrontShow)){
                                        fieldFrontShow = "vname";
                                    }
                                    dropdownList = baseInfoSimpleService.getDropdownData(fieldSearchType,fieldFrontSave,fieldFrontShow);
                                }
                            }
                            fieldsListMap.put("dropdownList", dropdownList);
                    		if (enableEdit == 0){dicFields.setFieldFrontEdit(false);}
                    		Map<String, Object> productBasisMap = TypeUtils.entityToMap(productBasis);
                    		fieldsListMap.put("fieldContent", productBasisMap.get(fieldsListMap.get("fields")));
                    		
                    		if (fieldsListMap.get("fieldFrontType") == null){
                    			fieldsListMap.put("fieldFrontType", "t_text");
                    		}
                    		
                    		if ((Integer)fieldsListMap.get("fieldFrontTop") == 1){	//显示的字段
                    			fieldsList.add(TypeUtils.mapToJson(fieldsListMap));
                    		}else {	//隐藏的字段
                    			fieldsHiddenList.add(TypeUtils.mapToJson(fieldsListMap));
                    		}
                    	}
                	}
                }
            }
            catch(Exception e){
                ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
            }
            //取商品子件列表网格
        	params.put("moduleTable", moduleTableZJ);
        	params.put("entity", new ProductBasisComponent()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
        	params.put("readOnly", enableEdit == 1 ? false : true);//生成列是否可编辑
        	params.put("toolBarWidth", enableEdit == 1 ? 200 : 0);//操作区
        	params.put("getFieldAttributesType", "pageingrid");//pageingrid：网格字段
            importFields = dictionaryForFieldService.queryFieldFrontImport(params); //导入模板
            mv.addObject("gridColumnsZJ",gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
            mv.addObject("importFields",importFields);				//导入字段模板
            //取竞品列表网格
        	params.put("moduleTable", moduleTableJP);
        	params.put("entity", new ProductBasisCompete()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
        	params.put("readOnly", enableEdit == 1 ? false : true);//生成列是否可编辑
        	params.put("toolBarWidth", enableEdit == 1 ? 200 : 0);//操作区
        	params.put("getFieldAttributesType", "pageingrid");//pageingrid：网格字段
            mv.addObject("gridColumnsJP",gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));

        	params.put("moduleTable", moduleTable);
            mv.addObject("pageType","edit"); //页面类型：新增（add）、修改（edit）
            mv.addObject("enableEdit",enableEdit); //当前页面是否可编辑（0：不可编辑，1：可编辑）
            mv.addObject("productBasis",productBasis); //商品库
            mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap)); //商品库各字段及属性
            mv.addObject("fieldsList", fieldsList);				//显示的字段
            mv.addObject("fieldsHiddenList", fieldsHiddenList);	//隐藏的字段
            mv.addObject("ajaxResult", ajaxResult);
            mv.setViewName(prefix + "/productbasis_edit");
        }
        return mv;
    }
    
    /**
     * 修改商品库--数据提交
     * @param productBasisItems 必要参数：商品库对应实体类（ProductBasis）的json格式字符串
     * @return AjaxResult
     * @author DH-Xxj
     */
    @Log(title = "修改商品", businessType = BusinessType.UPDATE)
    @RequiresPermissions("business:productbasis:edit")
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(@RequestParam Map<String, Object> params){
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.updateProductBasis(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 修改商品子件--数据提交
     * @param productBasisComponentItems 必要参数：商品子件对应实体类（ProductBasisComponent）的json格式字符串
     * @return AjaxResult
     * @author DH-Xxj
     */
    @Log(title = "修改商品子件", businessType = BusinessType.UPDATE)
    @RequiresPermissions("business:productbasis:edit")
    @PostMapping("/editzjpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editZJSave(@RequestParam Map<String, Object> params){
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.updateProductBasisComponent(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 修改竞品库--数据提交
     * @param params
     * @return AjaxResult
     * @author DH-Xxj
     */
    @Log(title = "修改竞品", businessType = BusinessType.UPDATE)
    @RequiresPermissions("business:productbasis:edit")
    @PostMapping("/editjppost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editJPSave(@RequestParam Map<String, Object> params){
    	AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.updateProductBasisCompete(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    
    /**
     * 删除商品库（完整删除，包含子件和竞品）
     * @param productBasisItems 必要参数：商品库对应实体类（ProductBasis）的json格式字符串
     * @return AjaxResult
     * @author DH-Xxj
     */
    @RequiresPermissions("business:productbasis:delete")
    @Log(title = "删除商品", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult productDelete(@RequestParam Map<String, Object> params){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.deleteProductBasisCompleteByGuid(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e)); 
        }
        return ajaxResult;
    }
    
    /**
     * 删除商品子件
     * @param productBasisComponentItems 必要参数：商品子件对应实体类（ProductBasisComponent）的json格式字符串
     * @return AjaxResult
     * @author DH-Xxj
     */
    @RequiresPermissions("business:productbasis:delete")
    @Log(title = "删除商品子件", businessType = BusinessType.DELETE)
    @PostMapping("/deletezj")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult productZJDelete(@RequestParam Map<String, Object> params){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.deleteProductBasisComponentByGuid(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e)); 
        }
        return ajaxResult;
    }
    
    /**
     * 删除竞品库
     * @param productBasisCompeteItems 必要参数：竞品库对应实体类（ProductBasisCompete）的json格式字符串
     * @return AjaxResult
     * @author DH-Xxj
     */
    @RequiresPermissions("business:productbasis:delete")
    @Log(title = "删除竞品", businessType = BusinessType.DELETE)
    @PostMapping("/deletejp")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult productJPDelete(@RequestParam Map<String, Object> params){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.deleteProductBasisCompeteByGuid(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e)); 
        }
        return ajaxResult;
    }
    
    /**
     * 从商品库调入选品
     * @param params
     * @return
     * @author DH-Xxj
     */
	@GetMapping("/callproductxplist")
//    @RequiresPermissions("business:productbasis:list")
    @ResponseBody
    public TableDataInfo callProductXPList(@RequestParam Map<String, Object> params){
		params.put("jurisdictionModuleTable", moduleTable);
        List<ProductBasis> list = new ArrayList<ProductBasis>();
        try {
        	list = productBasisService.queryCallProductXP(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }

	/**
	 * 根据不同的标签切换不同的列表
	 * @param params
	 * @return
	 * @author DH-Xxj
	 */
    @GetMapping("/switchover")
    @ResponseBody
    public Map<String, Object> switchover(@RequestParam Map<String, Object> params){
        Map<String, Object> dicFieldsList = new HashMap<String, Object>();
        String fixtype = "";
        if (params.get("num").equals("1")){
            params.put("moduleTable", moduleTable);
            params.put("entity", new ProductBasis()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
            fixtype = moduleTable;
        } else if (params.get("num").equals("2")){
            params.put("moduleTable", moduleTableZJ);
            params.put("entity", new ProductBasisComponent());
            fixtype = moduleTableZJ;
        }
        params.put("checkBox", "yes");
        params.put("toolBarWidth", 0);
        dicFieldsList=gridColumnsPropertyModel.queryGridColumnsPropertyModel(params);
        //返回相对应的查询条件
        dicFieldsList.put("fixtype",baseInfoSimpleService.getFieldFrontConditions(fixtype));
        return dicFieldsList;
    }


    /**
     * 修改商品状态(启/停)
     * @param productBasisItems 必要参数：商品库对应实体类（ProductBasis）的json格式字符串
     * @return AjaxResult
     * @author DH-Xxj
     */
    @RequiresPermissions("business:productbasis:delete")
    @Log(title = "updateState", businessType = BusinessType.DELETE)
    @PostMapping("/updateState")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult updateState(@RequestParam Map<String, Object> params){
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= productBasisService.updateState(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 生成商品标（PDF格式）
     * @param params
     * @return
     * @throws Exception 
     */
    @RequestMapping("/generateGoodsPDF")
    @ResponseBody
    public AjaxResult generateGoodsPDF(String productCode) throws Exception{
    	
    	if (productCode == null || productCode.length() == 0) {
			return AjaxResult.error("数据不存在");
		}

        AjaxResult ajaxResult = new AjaxResult();
        try {

            ProductBasis productBasis = productBasisService.queryProductBasisByCode(productCode);
            if (productBasis == null){
                return AjaxResult.error("单据不存在,不允许打印!");
            }

            // 生成PDF到指定目录下
            String productNameEn = productBasis.getNameEn();
            
            String path = productBasisService.createGoodsPdf(productCode, productNameEn, 82);

            ajaxResult = AjaxResult.success(path);
        }catch (Exception e) {
        	e.printStackTrace();
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
}
